<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>小米商城-提交订单</title>
	<link rel="stylesheet" type="text/css" href="css/reset.css">
	<link rel="stylesheet" type="text/css" href="css/main.css">
	<link rel="stylesheet" type="text/css" href="./css/style.css">
</head>
<body>
	<div class="header_con">
		<div class="header">
			<div class="welcome fl">欢迎来到小米商城</div>
			<div class="fr">
				<c:if test="${empty user}">
					<div class="login_btn fl">
						<a href="login.jsp">登录</a>
						<span>|</span>
						<a href="register.jsp">注册</a>
					</div>
				</c:if>
				<c:if test="${not empty user}">
					<div class="login_info fl" style="display:block">
						欢迎您：<em>${user.uname}</em>
					</div>
				</c:if>	
				<div class="user_link fl">
					<span>|</span>
					<a href="user_center_info.jsp">用户中心</a>
					<span>|</span>
					<a href="gouwuche.jsp">我的购物车</a>
					<span>|</span>
					<a href="user_center_order.jsp">我的订单</a>
				</div>
			</div>
		</div>		
	</div>

	<div class="search_bar clearfix">
		<a href="index.jsp" class="logo fl"><img src="image/logo_top.png" style="width:80px;"></a>
		<div class="sub_page_name fl">|&nbsp;&nbsp;&nbsp;&nbsp;提交订单</div>
			
	</div>
	
	<h3 class="common_title">确认收货地址</h3>

	<div class="common_list_con clearfix">
		<dl>
			<dt>寄送到：</dt>
			<dd id="addr"></dd>
		</dl>
		<a href="user_center_site.jsp" class="edit_site">编辑收货地址</a>

	</div>
	
	<h3 class="common_title">支付方式</h3>	
	<div class="common_list_con clearfix">
		<div class="pay_style_con clearfix">
			<input type="radio" name="dt" id="1" checked>
			<label class="cash">货到付款</label>
			<input type="radio" name="dt" id="2">
			<label class="weixin">微信支付</label>
			<input type="radio" name="dt" id="3">
			<label class="zhifubao"></label>
			<input type="radio" name="dt" id="4">
			<label class="bank">银行卡支付</label>
		</div>
	</div>

	<h3 class="common_title">商品列表</h3>
	
	<div class="common_list_con clearfix">
		<ul class="goods_list_th clearfix">
			<li class="col01">商品名称</li>
			<li class="col02">商品编号</li>
			<li class="col03">商品价格</li>
			<li class="col04">数量</li>
			<li class="col05">小计</li>		
		</ul>
		<div id="goods">
		
		</div>
		
	</div>

	<h3 class="common_title">总金额结算</h3>

	<div class="common_list_con clearfix">
		<div class="settle_con">
			<div class="total_goods_count">共<em id="tn">2</em>件商品，总金额<b id="tp">42.60元</b></div>
			<div class="transit">运费：<b id='yf'>10元</b></div>
			<div class="total_pay">实付款：<b id="ttp">52.60元</b></div>
		</div>
	</div>

	<div class="order_submit clearfix">
		<a href="javascript:void(0)" id="order_btn">提交订单</a>
	</div>	

	<footer class="mt20 center">			
		<div class="mt20">小米商城|MIUI|米聊|多看书城|小米路由器|视频电话|小米天猫店|小米淘宝直营店|小米网盟|小米移动|隐私政策|Select Region</div>
		<div>?mi.com 京ICP证110507号 京ICP备10046444号 京公网安备11010802020134号 京网文[2014]0059-0009号</div> 
		<div>违法和不良信息举报电话：185-0130-1238，本网站所列数据，除特殊说明，所有数据均出自我司实验室测试</div>
	</footer>

	<div class="popup_con">
		<div class="popup">
			<p>订单提交成功！</p>
		</div>
		
		<div class="mask"></div>
	</div>
	<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
	<script type="text/javascript">
		
	
	
		//提交订单的按钮   首先先将这几条数据从购物车表中删除在添加进订单表
		$('#order_btn').click(function() {
			//订单表需要订单号null,uid:用户号,aid:地址的id，gid:商品号 ,date:日期,allprice:此次订单的金额,支付状态 ptid:付款方式
			//console.log($(".goods_list_td .col04:eq(0)").text());
			
			var length=parseInt($("#goods").children("ul").length);//获取有几条数据
			
			//将所有的商品的gid循环添加进数组
			var str='';
			//获取小计   
			var strMoney='';
			//获取数量
			var strNum='';
			for(var i=0;i<length-1;i++){
				str+=$(".goods_list_td .col04:eq("+i+")").text()+",";
				strMoney+=$(".goods_list_td .col07:eq("+i+")").text()+",";
				strNum+=$(".goods_list_td .col06:eq("+i+")").text()+",";
			}
			str+=$(".goods_list_td .col04:eq("+(length-1)+")").text();
			strMoney+=$(".goods_list_td .col07:eq("+(length-1)+")").text();
			strNum+=$(".goods_list_td .col06:eq("+i+")").text();
			console.log(str+"-----"+strMoney+"----"+strNum);
			
			var ptid=parseInt($("input[name='dt']:checked").attr("id"));//获取支付方式的id
			
			//发ajax请求对数据进行修改
			$.post("orderServlet",{
				op:"addOrder",
				str:str,
				strMoney:strMoney,
				strNum:strNum,
				ptid:ptid,
				length:length
			},function(data){
				if(data>0){
					//添加成功
					alert("提交订单成功");
					location.href="user_center_order.jsp?page=1";
				}else if(data==-2){
					//没有收件地址打开新页面添加
					location.href='http://localhost:8080/Xiaomi/user_center_site.jsp';
				}else{
					alert("网络异常,稍后再试....");
				}
			},"text");
		});
	
	
	if( $(".login_btn").html() ){
		//提示用户先登录
		alert("请先登录");
		location.href="login.jsp";
	}	
	
		//页面一加载  就显示信息
		//首先获取地址栏的cid
		//0: "http://localhost/Xiaomi/place_order.jsp?"1: "cid=12" 2: "cid=13"
		var arr=location.href.split(",");
		var length=arr.length;
		var str='';
		for(var i=1;i<length-1;i++){
			str+=arr[i].split("=")[1]+",";
		}
		str+=arr[length-1].split("=")[1];
		console.log(str);
		//发ajax  首先在订单页面显示你的信息   
		$.post("cartinfoServlet",{
			op:"showorder",
			str:str
		},function(data){
			var str='';
			var totalnum=0;
			var totalprice=0;
			if(data.length>0){
				for(var i=0;i<data.length;i++){
					str+='<ul class="goods_list_td clearfix"><li class="col01">'+(i+1)+'</li>';
					str+='<li class="col02"><img src="'+data[i].pic+'"></li>';
					str+='<li class="col03">'+data[i].tname+'</li><li class="col04">'+data[i].gid+'</li>';
					str+='<li class="col05">'+data[i].price+'元</li><li class="col06">'+data[i].num+'</li>';
					str+='<li class="col07">'+data[i].allprice+'</li></ul>';
					totalnum=i+1;
					totalprice+=parseInt(data[i].allprice);
				}
			}
			$("#tn").html(totalnum);
			$('#tp').html(totalprice);
			$("#goods").html(str);
			
			if(totalprice>5000){
				$("#yf").html(0);
			}else{
				$("#yf").html(10);
			}
			
			$("#ttp").html(totalprice+parseInt($("#yf").html()));
		},"json");
		
		//页面一加载就发请求 获取默认地址
		$.post("addrServlet",{
			op:"getDafalutAddr"
		},function(data){
			var str='<input type="radio" name="" checked="">';
			if( data.length<=0 ){
				alert("请先添加收货地址");
				location.href="user_center_site.jsp?page=1";
			}else{
				for(var i=0;i<data.length;i++){
					str+=''+data[i].province+' '+data[i].city+' '+data[i].area+' '+data[i].addr+' （'+data[i].name+' 收） '+data[i].tel;
				}
			}
		$("#addr").html(str);
		},"json");
		
		
	</script>
		
</body>
</html>